package com.solarStruggle.controls

import scala.swing.{Frame, Publisher}
import scala.swing.event.{Event}
import domain.{Game, SolarSystem}

/**
 * Notification that the user has changed the zoom level
 */
case class ZoomLevelChanged(newValue: Int) extends Event

/**
 * Notification that the user has changed the current game
 */
case class CurrentGameChanged(newValue: Option[Game]) extends Event

/**
 * Notification that the user has changed to a new solar system
 */
case class CurrentSolarSystemChanged(newValue: Option[SolarSystem]) extends Event

/**
 * Object-level constants used when determining the current zoom factor
 */
object GuiState {
  
  /**
   * The factors to scale the main image by based on the current zoom level
   */
  val zoomFactors = 0.125f :: 0.25f :: 0.5f :: 0.75f :: 1.0f :: Nil
  
}  

/**
 * The current state of the views.
 */
class GuiState(val rootFrame: Frame)
extends Publisher {
  
  /**
   * How to scale the main image
   */
  private var _zoomLevel: Int = minZoomLevel
  
  def zoomLevel = _zoomLevel
  def zoomLevel_=(value: Int) { _zoomLevel = value; publish(ZoomLevelChanged(value)) }
  
  val minZoomLevel = resetZoomLevel
  val maxZoomLevel = GuiState.zoomFactors.length - 1
  val resetZoomLevel = maxZoomLevel - 2
  
  def zoomFactor = GuiState.zoomFactors(zoomLevel)
  
  private var _currentGame: Option[Game] = None
  def currentGame = _currentGame
  def currentGame_=(value: Option[Game]) { _currentGame = value; publish(CurrentGameChanged(value)) }
  
  private var _currentSolarSystem: Option[SolarSystem] = None
  def currentSolarSystem = _currentSolarSystem
  def currentSolarSystem_=(value: Option[SolarSystem]) { _currentSolarSystem = value; publish(CurrentSolarSystemChanged(value)) }
}
